//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.imu.purse.modules.sms;

import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import com.imu.common.exception.ErrorDesc;
import com.imu.common.validator.Assert;

@Component
public class SMSCache
{
    private static final String SMS_AUTHCODE_KEY = "sms:acode:";
    private static final String SMS_FLAG = ":flag";
    public static final int SMS_AUTHCODE_TIMEOUT = 300;
    public static final int SMS_RESEND_TIMEOUT = 60;
    private static final String SMS_REMIND_KEY = "sms:remind:";

    public SMSCache() {}

    public String check(String phone) 
    {
    	Assert.isBlank(phone, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	
    	String key = SMS_AUTHCODE_KEY + phone + SMS_FLAG;
        return redisTemplate.opsForValue().get(key) == null ? "0" : "1";
    }

    public String saveCode(String t, String phone, String acode)
    {
    	Assert.isBlank(t,     ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	Assert.isBlank(acode, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	Assert.isBlank(phone, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	
    	ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String key = SMS_AUTHCODE_KEY + phone;
        
        operations.set(key + ":" + t, acode, SMS_AUTHCODE_TIMEOUT, TimeUnit.SECONDS);
        operations.set(key + SMS_FLAG, "0", SMS_RESEND_TIMEOUT, TimeUnit.SECONDS);
        return "1";
    }

    public String findCode(String t, String phone)
    {
    	Assert.isBlank(t,     ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	Assert.isBlank(phone, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	String key = SMS_AUTHCODE_KEY + phone;
        return redisTemplate.opsForValue().get(key + ":" + t);
    }
    
    //删除缓存信息
    public void delCode(String t, String phone) 
    {
    	Assert.isBlank(t,     ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	Assert.isBlank(phone, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
    	String key = SMS_AUTHCODE_KEY + phone;
        redisTemplate.delete(key + ":" + t);
    }

    /**
     * 提醒缓存
     * @param phone
     * @param content
     * @return
     */
    public void saveRemindData(String phone,String type,String content){
        Assert.isBlank(phone,     ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
        Assert.isBlank(content, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
        Assert.isBlank(type, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());

        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String key = SMS_REMIND_KEY + type + ":" + phone;
        operations.set(key , content, SMS_AUTHCODE_TIMEOUT, TimeUnit.SECONDS);

    }

    /**
     * 获取提醒的信息
     * @param phone
     * @return
     */
    public String findRemindData(String phone,String type){
        Assert.isBlank(phone,     ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());
        Assert.isBlank(type, ErrorDesc.PARAM_EMPTY.getMsg(), ErrorDesc.PARAM_EMPTY.getCode());

        String key = SMS_REMIND_KEY + type + ":" + phone;
        return redisTemplate.opsForValue().get(key);
    }
    
	@Autowired
    private RedisTemplate<String, String> redisTemplate;
}
